package com.coocaa.ops.admin.v1.auth.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysDeptBiz;
import com.coocaa.ops.admin.v1.auth.sys.biz.ISysUserBiz;
import com.coocaa.ops.admin.v1.auth.sys.model.query.SysDeptModel;
import com.coocaa.ops.admin.v1.auth.sys.service.ISysDeptService;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.auth.entity.SysDeptEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/3/12.
 * @description: 系统部门 service接口 实现
 */
@Service
public class SysDeptServiceImpl implements ISysDeptService {
    @Autowired
    private ISysDeptBiz sysDeptBiz;

    @Autowired
    private ISysUserBiz sysUserBiz;
    private static CommonLanguageProperties commonLanguageProperties;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysDeptEntity entity) {
        if (null == entity.getId()) {
            sysDeptBiz.add(entity);
        } else {
            sysDeptBiz.updateById(entity);
        }
    }

    @Override
    public SysDeptEntity getDetailInfo(Integer id) {
        SysDeptEntity query = sysDeptBiz.queryById(id);
        return query;
    }

    @Override
    public Map<String, Object> getPageResult(SysDeptModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        PageResultModel<SysDeptEntity> pageResult = sysDeptBiz.getCommonPageList(params, false);
        return R.ok(pageResult);
    }

    /**
     * 获取所有可用部门
     *
     * @return
     */
    @Override
    public Map<String, Object> listAllDept() {
        Map<String, Object> params = new HashMap<>(1);
        params.put("disabled", 0);
        List<SysDeptEntity> list = sysDeptBiz.getMapper().queryCommonPageList(params);
        return R.ok(list);
    }

    /**
     * 更新排序
     *
     * @param id
     * @param seq
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSeq(Integer id, Integer seq) {
        SysDeptEntity update = new SysDeptEntity();
        update.setId(id);
        update.setSeq(seq);
        sysDeptBiz.updateById(update);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Map deleteBatch(Integer[] ids, SysUserEntity user) {
        String result = this.deleteBatch(ids, user.getId());
        if (R.SUCCESS.equals(result)) {
            return R.ok().msg(commonLanguageProperties.getDeleteSuccess());
        } else {
            return R.fail(result);
        }
    }

    @Autowired
    public void setEnterKeyLetterArt(CommonLanguageProperties commonLanguageProperties) {
        SysDeptServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/
    /**
     * 处理批量删除
     *
     * @param ids
     * @param userId
     * @return
     */
    private String deleteBatch(Integer[] ids, Integer userId) {
        if (ArrayUtils.isNotEmpty(ids)) {
            if (!areDeptsEmpty(ids)) {
                return "There are users in the department and cannot be deleted";
            }
            UpdateWrapper<SysDeptEntity> wrapper = new UpdateWrapper<>();
            wrapper.in("id", ids);
            SysDeptEntity update = new SysDeptEntity();
            update.setDisabled(2);
            sysDeptBiz.getMapper().update(update, wrapper);
        }
        return R.SUCCESS;
    }

    /**
     * 检查部门是否为空，为空才可删除
     *
     * @param deptIds
     * @return
     */
    private boolean areDeptsEmpty(Integer[] deptIds) {
        for (Integer id : deptIds) {
            Integer count = sysUserBiz.getUserCountByDept(id);
            if (count != null && count > 0) {
                return false;
            }
        }
        return true;
    }
}
